package com.book.bookmanager.controller;

import com.book.bookmanager.Tool.FileUtil;
import com.book.bookmanager.pojo.Book;
import com.book.bookmanager.pojo.Head;
import com.book.bookmanager.service.headService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Controller
@RequestMapping("book")
public class indexController {
	@Autowired
	private headService hs;
/**
	//跳转到上传文件的页面
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String goUploadImg() {
		return "index";
	}

	@RequestMapping("doSuccess")
	public String doSuccess(Model model){
		List<Head> list = hs.queryAll();
		model.addAttribute("list", list);
		return "success";
	}
	//处理上传的文件
	@PostMapping("/doUpload")
	public String uploadImg(Head head,
							// 定义一个 MultipartFile 类型的参数 file
							@RequestParam("file") MultipartFile file,
							Model model) {
		// 如果图片为空，显示error
		if ((file.getOriginalFilename().isEmpty())) {
			model.addAttribute("error", "error");
			return "index";
		} else {
			// 获取图片的名字
			String fileName = file.getOriginalFilename();
			// 图片存放的文件夹地址
			String filePath = "C:\\Users\\lsx\\Desktop\\";
			// 文件夹地址 + 名字 = 图片的路径
			String fileAddress = filePath + fileName;
			try {
				// 图片上传的工具类
				// 参数一：图片的编码格式的字节数组
				// 参数二：图片存放的文件夹地址
				// 参数三：图片的名字
				FileUtil.uploadFile(file.getBytes(), filePath, fileName);

				// 把图片路径写入数据库
				head.setHeadAddress(fileAddress);
				hs.addheadAddress(head);
			} catch (Exception e) {
			}

			return "redirect:/book/doSuccess";
		}
	}

*/
}
